package com.zegome.game.gomestory.main.scene.play.board;

import java.util.Random;
import java.util.Vector;

import android.util.Log;
import aurelienribon.tweenengine.equations.Quart;

import com.badlogic.gdx.math.Vector2;
import com.zegome.game.action.interpolation.SineOutFunction;
import com.zegome.game.action.percentage.IPercentageAction;
import com.zegome.game.action.percentage.IPercentageAction.IPercentageActionListener;
import com.zegome.game.entity.IEntity;
import com.zegome.game.entity.action.group.MovePercentageAction;
import com.zegome.game.entity.action.group.PercentageEntityGroupAction;

public class EffectArrangeMap {
	
	public Board board;	
	
	public EffectArrangeMap(Board board)
	{
		this.board = board;
	}
	
	public void start()
	{
		
		final PercentageEntityGroupAction groupAction = new PercentageEntityGroupAction(1.0f,null, SineOutFunction.getInstance());		
		int xRand, yRand;
		for(int row=0;row<10;row++){
			for(int col=0;col<14;col++){
				if(board.dataMap.map[row][col].type != -1)
				{
					xRand  = new Random().nextInt((int)(800));
					yRand = new Random().nextInt((int)(480));
					board.dataMap.map[row][col].setPosition(xRand, yRand);
					MovePercentageAction percentageAction = new MovePercentageAction(board.dataMap.map[row][col],xRand, board.dataMap.xBegin+board.dataMap.wi1Pikachu*col, yRand,board.dataMap.yBegin+board.dataMap.hi1Pikachu*row);
					percentageAction.addActionListener(new IPercentageActionListener<IEntity>() {
						
						@Override
						public void onActionStarted(IPercentageAction<IEntity> arg0, IEntity arg1) {
							// TODO Auto-generated method stub																					
							((Pikachu)arg1).isMove = true;
						}
						
						@Override
						public void onActionFinished(IPercentageAction<IEntity> arg0, IEntity arg1) {
							// TODO Auto-generated method stub							
							((Pikachu)arg1).isMove = false;
						}
					});
					groupAction.add(percentageAction);
					
//					Vector2 from = new Vector2(new Random().nextInt((int)(800)), new Random().nextInt((int)(480)));
//					Vector2 to = new Vector2(board.dataMap.xBegin+board.dataMap.wi1Pikachu*col,
//							board.dataMap.yBegin+board.dataMap.hi1Pikachu*row);
//					board.dataMap.map[row][col].runFromTo(1f, from, to);
				}
			}
		}
		board.screen.registerUpdateHandler(groupAction);
	}

	public void changeMap(){
		
		int i,j,r,c;
		
		//Random rand = new Random();
		
		Vector<Possition> possition = new Vector<Possition>();
		
		Possition pos1, pos2;

		int temp;
		
		Vector2 from,to;
		
		do {
			Log.e("AAAAAAAAAA", "BBBBBBB");
			Random rand = new Random();
			for(r=1;r<=board.dataMap.mapRow;r++){
				for(c=1;c<=board.dataMap.mapCol;c++){
					if(board.dataMap.map[r][c].type!=-1){
						possition.add(new Possition(r, c));
					}
				}
			}	
			if(possition.size() == 4){
				i = 0;
				pos1 = new Possition(possition.elementAt(i).row, possition.elementAt(i).col);
				possition.removeElementAt(i);
				
				j = 1;
				pos2 = new Possition(possition.elementAt(j).row, possition.elementAt(j).col);
				possition.removeElementAt(j);
				
				temp = board.dataMap.map[pos1.row][pos1.col].type;							
				board.dataMap.map[pos1.row][pos1.col].type = board.dataMap.map[pos2.row][pos2.col].type;				
				from = new Vector2(board.dataMap.xBegin+board.dataMap.wi1Pikachu*pos2.col,
						board.dataMap.yBegin+board.dataMap.hi1Pikachu*pos2.row);
				to = new Vector2(board.dataMap.xBegin+board.dataMap.wi1Pikachu*pos1.col,
						board.dataMap.yBegin+board.dataMap.hi1Pikachu*pos1.row);
				board.dataMap.map[pos1.row][pos1.col].runFromTo(1f, from, to);

				board.dataMap.map[pos2.row][pos2.col].type = temp;
				from = new Vector2(board.dataMap.xBegin+board.dataMap.wi1Pikachu*pos1.col,
						board.dataMap.yBegin+board.dataMap.hi1Pikachu*pos1.row);
				to = new Vector2(board.dataMap.xBegin+board.dataMap.wi1Pikachu*pos2.col,
						board.dataMap.yBegin+board.dataMap.hi1Pikachu*pos2.row);
				board.dataMap.map[pos2.row][pos2.col].runFromTo(1f, from, to);
			}
			else{
			while (possition.size()>0) {				
				i = rand.nextInt(possition.size());
				pos1 = new Possition(possition.elementAt(i).row, possition.elementAt(i).col);
				possition.removeElementAt(i);
				
				j = rand.nextInt(possition.size());
				pos2 = new Possition(possition.elementAt(j).row, possition.elementAt(j).col);
				possition.removeElementAt(j);
				
				temp = board.dataMap.map[pos1.row][pos1.col].type;							
				board.dataMap.map[pos1.row][pos1.col].type = board.dataMap.map[pos2.row][pos2.col].type;				
				from = new Vector2(board.dataMap.xBegin+board.dataMap.wi1Pikachu*pos2.col,
						board.dataMap.yBegin+board.dataMap.hi1Pikachu*pos2.row);
				to = new Vector2(board.dataMap.xBegin+board.dataMap.wi1Pikachu*pos1.col,
						board.dataMap.yBegin+board.dataMap.hi1Pikachu*pos1.row);
				board.dataMap.map[pos1.row][pos1.col].runFromTo(1f, from, to);

				board.dataMap.map[pos2.row][pos2.col].type = temp;
				from = new Vector2(board.dataMap.xBegin+board.dataMap.wi1Pikachu*pos1.col,
						board.dataMap.yBegin+board.dataMap.hi1Pikachu*pos1.row);
				to = new Vector2(board.dataMap.xBegin+board.dataMap.wi1Pikachu*pos2.col,
						board.dataMap.yBegin+board.dataMap.hi1Pikachu*pos2.row);
				board.dataMap.map[pos2.row][pos2.col].runFromTo(1f, from, to);
			}	
			}
		}
		while (board.searchEat.search()==null);
	}
}
